package com.itboye.sunsun_china.www.aq;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.Switch;
import android.widget.TextView;

import com.itboye.sunsun_china.www.aq.base.AppConfig;
import com.itboye.sunsun_china.www.aq.base.AqDevice;
import com.itboye.sunsun_china.www.aq.base.AqDeviceMessage;
import com.itboye.sunsun_china.www.aq.base.AqFaultState;
import com.itboye.sunsun_china.www.aq.base.MessageWhat;
import com.itboye.sunsun_china.www.aq.base.MyApplication;

/**
 * 日期：2015-04-30
 * 		1. 针对不同的设备实现不同的可控功能
 * 日期：2015-05-11
 * 		1. 添加设置鱼缸水温功能
 * 		2. AQ107禁止设置鱼缸水温
 * 		3. 修改报警图标指示方式
 * 日期：2015-11-20
 * 		1. 解决Switch拖动时候不能产生setOnCheckedChangeListener事件的BUG
 */
@SuppressLint("NewApi") public class DeviceOptionActivity extends ActionBarActivity {

	MyApplication mApp;
	AqDevice mDevice;

	Button mButtonPeriod;
	Button mButtonTempMax;
	Button mButtonTempMin;
	Button mButtonSetTemp;
	int mTempMax;
	int mTempMin;
	int mSetTemp;
	SwitchImageButton mButtonWarnLamp;
	SwitchImageButton mButtonWarnPump;
	SwitchImageButton mButtonWarnUvLamp;
	SwitchImageButton mButtonWarnStandbyPower;
	Switch mSwitchWarnTemp;
	Switch mSwitchWarnLevel;
	Switch mSwitchWarnLoadDevice;
	Switch mSwitchLock;
	TextView mTextViewMaxTemp;
	TextView mTextViewMinTemp;
	TextView mTextViewSetTemp;
	int mNewTempValue;
	ImageView mImageViewWarnTemp;
	ImageView mImageViewWarnLevel;
	ImageView mImageViewWarnLamp;
	ImageView mImageViewWarnPump;
	ImageView mImageViewWarnUvLamp;
	ImageView mImageViewWarnStandbyPower;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_device_option);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setDisplayShowTitleEnabled(true);

		mApp = (MyApplication) getApplication();
		mDevice = mApp.mCurrentDevice;
		if (mDevice == null) {
			this.finish();
		}
		mApp.mDeviceOptionUi = this;

		mButtonTempMax = (Button) findViewById(R.id.device_option_button1);
		mButtonTempMin = (Button) findViewById(R.id.device_option_button2);
		mButtonPeriod = (Button) findViewById(R.id.device_option_button3);
		mButtonSetTemp = (Button) findViewById(R.id.device_option_button4);

		mButtonWarnLamp = (SwitchImageButton) findViewById(R.id.device_option_imageButton1);
		mButtonWarnPump = (SwitchImageButton) findViewById(R.id.device_option_imageButton2);
		mButtonWarnUvLamp = (SwitchImageButton) findViewById(R.id.device_option_imageButton3);
		mButtonWarnStandbyPower = (SwitchImageButton) findViewById(R.id.device_option_imageButton4);

		mSwitchWarnTemp = (Switch) findViewById(R.id.device_option_switch1);
		mSwitchWarnLevel = (Switch) findViewById(R.id.device_option_switch2);
		mSwitchWarnLoadDevice = (Switch) findViewById(R.id.device_option_switch3);
		mSwitchLock = (Switch) findViewById(R.id.device_option_switch4);

		mTextViewMaxTemp = (TextView) findViewById(R.id.device_option_textView8);
		mTextViewMinTemp = (TextView) findViewById(R.id.device_option_textView10);
		mTextViewSetTemp = (TextView) findViewById(R.id.device_option_textView17);

		mImageViewWarnTemp = (ImageView) findViewById(R.id.device_option_imageView_warn1);
		mImageViewWarnLevel = (ImageView) findViewById(R.id.device_option_imageView_warn2);
		mImageViewWarnLamp = (ImageView) findViewById(R.id.device_option_imageView_warn3);
		mImageViewWarnPump = (ImageView) findViewById(R.id.device_option_imageView_warn4);
		mImageViewWarnUvLamp = (ImageView) findViewById(R.id.device_option_imageView_warn5);
		mImageViewWarnStandbyPower = (ImageView) findViewById(R.id.device_option_imageView_warn6);

		mTempMax = mDevice.mMaxFaultTemp / 10;
		mTempMin = mDevice.mMinFaultTemp / 10;
		mSetTemp = mDevice.mMaxTemp / 10;
		mNewTempValue = 0;
		showSetTemp(mSetTemp);
		showFaultMaxTemp(mTempMax);
		showFaultMinTemp(mTempMin);
		showConfigTempState(mDevice.mSubscribeTempState);
		showConfigLevelState(mDevice.mSubscribeLevelState);
		showConfigLoadDeviceState(mDevice.mSubscribeLoadState);
		showConfigLockState(mDevice.mLockState);
		showWarnState(mDevice.mFaultState);
		mButtonPeriod.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent mainIntent = new Intent(DeviceOptionActivity.this,
						PeriodListActivity.class);
				DeviceOptionActivity.this.startActivity(mainIntent);
			}
		});

		mButtonSetTemp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				NumberPicker mPicker = new NumberPicker(DeviceOptionActivity.this);
				mPicker.setMinValue(AppConfig.Temp_Set_Min);
				mPicker.setMaxValue(AppConfig.Temp_Set_Max);
				mPicker.setValue(mSetTemp);
				mPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
					@Override
					public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
						mNewTempValue = newVal;
					}
				});
				AlertDialog mAlertDialog = new AlertDialog.Builder(DeviceOptionActivity.this)
						.setTitle("请选择鱼缸水温").setView(mPicker).setPositiveButton("取消", null).setNegativeButton("确定", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								mSetTemp = mNewTempValue;
								showSetTemp(mSetTemp);
								setTemp(mSetTemp);
							}
						}).create();
				mAlertDialog.show();
			}
		});

		mButtonTempMax.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				NumberPicker mPicker = new NumberPicker(DeviceOptionActivity.this);
				mPicker.setMinValue(AppConfig.Temp_Fault_Min);
				mPicker.setMaxValue(AppConfig.Temp_Fault_Max);
				mPicker.setValue(mTempMax);
				mPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
					@Override
					public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
						mNewTempValue = newVal;
					}
				});
				AlertDialog mAlertDialog = new AlertDialog.Builder(DeviceOptionActivity.this)
						.setTitle("请选择最高温度").setView(mPicker).setPositiveButton("取消", null).setNegativeButton("确定", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								mTempMax = mNewTempValue;
								showFaultMaxTemp(mTempMax);
								setPushMaxTemp(mTempMax);
							}
						}).create();
				mAlertDialog.show();
			}
		});

		mButtonTempMin.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				NumberPicker mPicker = new NumberPicker(DeviceOptionActivity.this);
				mPicker.setMinValue(AppConfig.Temp_Fault_Min);
				mPicker.setMaxValue(AppConfig.Temp_Fault_Max);
				mPicker.setValue(mTempMin);
				mPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
					@Override
					public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
						mNewTempValue = newVal;
					}
				});
				AlertDialog mAlertDialog = new AlertDialog.Builder(DeviceOptionActivity.this)
						.setTitle("请选择最低温度").setView(mPicker).setPositiveButton("取消", null).setNegativeButton("确定", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								mTempMin = mNewTempValue;
								showFaultMinTemp(mTempMin);
								setPushMinTemp(mTempMin);
							}
						}).create();
				mAlertDialog.show();
			}
		});

		mSwitchWarnTemp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				//if (!mSwitchWarnTemp.isPressed()) return;
				setPushTemp(isChecked);
			}
		});

		mSwitchWarnLevel.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				//if (!mSwitchWarnLevel.isPressed()) return;
				setPushLevel(isChecked);
			}
		});

		mSwitchWarnLoadDevice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				//if (!mSwitchWarnLoadDevice.isPressed()) return;
				setPushLoadDevice(isChecked);
			}
		});

		mSwitchLock.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				//if (!mSwitchLock.isPressed()) return;
				setLock(isChecked);
			}
		});

		// 不同设备设置处理，目前支持AQ107、AQ209和AQ805
		switch (mApp.mCurrentDevice.mDeviceInfo.mDeviceType)
		{
			case AQ107:
				// AQ107禁止设置鱼缸水温
				showSetTemp(0);
				mButtonSetTemp.setEnabled(false);
			case AQ209:
			case AQ208:
				// AQ107和AQ209禁止设置报警推送
				mSwitchWarnTemp.setEnabled(false);
				mSwitchWarnLevel.setEnabled(false);
				mSwitchWarnLoadDevice.setEnabled(false);
				mButtonTempMax.setEnabled(false);
				mButtonTempMin.setEnabled(false);
				break;
			case AQ211:
				mSwitchWarnTemp.setEnabled(false);
				mSwitchWarnLevel.setEnabled(false);
				mSwitchWarnLoadDevice.setEnabled(false);
				mButtonTempMax.setEnabled(false);
				mButtonTempMin.setEnabled(false);
				mButtonSetTemp.setEnabled(false);
				mButtonPeriod.setEnabled(false);
				break;
			case AQ805:
				break;
			default:
				break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_device_option, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == android.R.id.home) {
			// 返回
			mApp.mDeviceOptionUi = null;
			this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 如果是返回键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			mApp.mDeviceOptionUi = null;
			this.finish();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void setPushTemp(boolean value) {
		mDevice.setSubscribeTemp(value);
	}

	private void setPushLevel(boolean value) {
		mDevice.setSubscribeLevel(value);
	}

	private void setPushLoadDevice(boolean value) {
		mDevice.setSubscribeLoad(value);
	}

	private void setLock(boolean value) {
		mDevice.setLock(value);
	}

	private void setPushMaxTemp(int value) {
		mDevice.setFaultTemp(value * 10, mTempMin * 10);
	}

	private void setTemp(int value) {
		mDevice.setTemp(value * 10);
	}

	private void setPushMinTemp(int value) {
		mDevice.setFaultTemp(mTempMax * 10, value * 10);
	}

	private void showWarnState(AqFaultState faultState) {
		if (faultState.mTemp != AqFaultState.TempFaultState.NORMAL) {
			mImageViewWarnTemp.setVisibility(View.VISIBLE);
		} else {
			mImageViewWarnTemp.setVisibility(View.INVISIBLE);
		}

		if (faultState.mLevel) {
			mImageViewWarnLevel.setVisibility(View.VISIBLE);
		} else {
			mImageViewWarnLevel.setVisibility(View.INVISIBLE);
		}

		if (faultState.mLamp) {
			mImageViewWarnLamp.setVisibility(View.VISIBLE);
		} else {
			mImageViewWarnLamp.setVisibility(View.INVISIBLE);
		}

		if (faultState.mPump) {
			mImageViewWarnPump.setVisibility(View.VISIBLE);
		} else {
			mImageViewWarnPump.setVisibility(View.INVISIBLE);
		}

		if (faultState.mUvLamp) {
			mImageViewWarnUvLamp.setVisibility(View.VISIBLE);
		} else {
			mImageViewWarnUvLamp.setVisibility(View.INVISIBLE);
		}

		if (faultState.mStandbyPower) {
			mImageViewWarnStandbyPower.setVisibility(View.VISIBLE);
		} else {
			mImageViewWarnStandbyPower.setVisibility(View.INVISIBLE);
		}
	}

	private void showConfigTempState(boolean value) {
		mSwitchWarnTemp.setChecked(value);
	}

	private void showConfigLevelState(boolean value) {
		mSwitchWarnLevel.setChecked(value);
	}

	private void showConfigLoadDeviceState(boolean value) {
		mSwitchWarnLoadDevice.setChecked(value);
	}

	private void showConfigLockState(boolean value) {
		mSwitchLock.setChecked(value);
	}

	private void showFaultMaxTemp(int value) {
		mTextViewMaxTemp.setText(String.valueOf(value) + "℃");
	}

	private void showSetTemp(int value) {
		mTextViewSetTemp.setText(String.valueOf(value) + "℃");
	}

	private void showFaultMinTemp(int value) {
		mTextViewMinTemp.setText(String.valueOf(value) + "℃");
	}

	public void newMessage(Message msg) {
		try {
			if (msg.what == MessageWhat.DeviceMessage) {
				AqDeviceMessage msgObj = (AqDeviceMessage) msg.obj;
				switch (msgObj.mMessageType) {
					case GetStateOk:
						break;
					default:
						break;
				}
			}
		} catch (Exception e) {
		}
	}
}
